using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;

public class BuffDisplay:MonoBehaviour {

	const float xDistance = 0.03125f;
	const float yDistance = 0.40625f;

	[SerializeField] Image image;
	[SerializeField] Image black;
	[SerializeField] TextMeshProUGUI text;

	static Queue<BuffDisplay> pool = new Queue<BuffDisplay>();
	static GameObject prefab;
	
	public static BuffDisplay Create(Sprite sprite,Transform parent) {

		if(prefab==null) prefab=Resources.Load<GameObject>("BuffDisplay");

		BuffDisplay result;
		if(pool.Count!=0) {
			result=pool.Dequeue();
			result.gameObject.SetActive(true);
		} else {
			GameObject newObject = Instantiate(prefab);
			result=newObject.GetComponent<BuffDisplay>();
		}
		result.image.sprite=sprite;
		result.transform.parent=parent;
		return result;
	}

	public void UpdateStats(float amount,int x,int xTotal,int y) {
		text.text=Mathf.CeilToInt(amount).ToString();
		black.fillAmount=1-(amount-Mathf.Floor(amount));
		if(black.fillAmount>=0.9999f) black.fillAmount=0;
		transform.localPosition=new Vector3((x-xTotal*0.5f+0.5f)*xDistance,(y+1)*yDistance);
		transform.localEulerAngles=new Vector3(0,0,0);
	}

	public void Remove() {
		gameObject.SetActive(false);
		transform.parent=Utility.dynamicParent.transform;
		pool.Enqueue(this);
	}

}
